import axios from "axios";
import {basicUrlDev, basicUrlLocal} from "../config/config";

export const api = axios.create({
    baseURL: basicUrlDev,
    timeout: 10000,
});

api.interceptors.request.use(config => {
    const token = localStorage.getItem('token');

    // 排除不需要携带 token 的接口
    // const url = config.url || '';
    // if (!noTokenPaths.includes(url) && token) {
    //     config.headers.Authorization = token;
    // }
    if (token) {
        config.headers.Authorization = token;
    }
    return config;
});

// 添加响应拦截器：处理401未授权
api.interceptors.response.use(
    res => res,
    err => {
        if (err.response?.status === 401) {
            // token失效，清除本地存储并跳转到登录页
            localStorage.removeItem('token');
            window.location.href = '/login';
        }
        return Promise.reject(err);
    }
);
